/*******************************************************************************
 * Copyright (c) 2012 Alexandr Tsvetkov.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Alexandr Tsvetkov - initial API and implementation
 * 
 * Project:
 *     TAO Library
 * 
 * File name:
 *     TSoundPoolHandler.java
 *     
 * License agreement:
 *
 * 1. This code is published AS IS. Author is not responsible for any damage that can be
 *    caused by any application that uses this code.
 * 2. Author does not give a garantee, that this code is error free.
 * 3. This code can be used in NON-COMMERCIAL applications AS IS without any special
 *    permission from author.
 * 4. This code can be modified without any special permission from author IF AND ONLY IF
 *    this license agreement will remain unchanged.
 * 5. SPECIAL PERMISSION for this code usage in COMMERCIAL application SHOULD be obtained
 *    from author.
 ******************************************************************************/
package ua.at.tsvetkov.media;

import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;

/**
 * @author A.Tsvetkov 2010 http://tsvetkov.at.ua mailto:al@ukr.net
 */
public class TSoundPoolHandler implements Runnable {

	private TMediaPlayer[]			players;
	private boolean					mustDone;
	long									preloadTime;

	/**
	 * Обработчик загрузки звуков
	 * 
	 * @param players проигрыватели
	 * @param preloadTime пауза для предварительной загрузки
	 */
	public TSoundPoolHandler(TMediaPlayer[] players, long preloadTime) {
		this.players = players;
		this.preloadTime = preloadTime;
		mustDone = false;
	}

	/**
	 * Останавливает обслуживание проигрывателей
	 */
	public void release() {
		mustDone = true;
	}

	@Override
	public void run() {
		while (!mustDone) {
			long time = System.currentTimeMillis();
			for (int i = 0; i < players.length; i++) {
				final int fi = i;
				// Плэйер нуждается в загрузке
				if (players[i].status == TMediaPlayer.NEED_TO_LOAD && players[i].timeBegin - preloadTime >= time) {
					try {
						players[i].reset();
						players[i].setDataSource(players[i].resourceID);
						players[i].prepareAsync();
						players[i].timeEnd = 0;
						players[i].status = TMediaPlayer.LOAD;
						players[i].setOnPreparedListener(new OnPreparedListener() {

							@Override
							public void onPrepared(MediaPlayer mp) {
								players[fi].status = TMediaPlayer.READY;
								players[fi].timeEnd = players[fi].timeBegin + players[fi].getDuration() + TSoundPool.ADDITIONAL_TIME;
							}

						});

					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				// Плейер простаивает
				if (players[i].timeEnd >= time) {
					players[i].status = TMediaPlayer.IDLE;
				}
			}
			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

}
